package com.adcc.ags.cgw.managed.proxy;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.routing.FromConfig;
import com.adcc.ags.cgw.service.*;
import com.adcc.utility.log.Log;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.setup.Environment;

/**
 * 报文代理Managed
 */
public class MsgProxy implements Managed {

    // 单例对象
    private static MsgProxy instance;

    // 配置文件
    private String proxyConf;

    // ActorSystem
    private ActorSystem actorSystem;

    // 消息接收Actor
    private ActorRef msgRecvActor;

    // 消息校验Actor
    private ActorRef msgVerifyActor;

    // 消息过滤Actor
    private ActorRef msgFilterActor;

    // 消息路由Actor
    private ActorRef msgRouteActor;

    // 消息发送Actor
    private ActorRef msgSendActor;

    // 消息入库Actor
    private ActorRef msgSaveActor;

    /**
     * 构造方法
     * */
    private MsgProxy() {}

    /**
     * 单例模式
     * */
    public synchronized static MsgProxy getInstance() {
        if (instance == null) {
            instance = new MsgProxy();
        }
        return instance;
    }

    public void setProxyConf(String proxyConf) {
        this.proxyConf = proxyConf;
    }

    public ActorRef getMsgRecvActor() {
        return msgRecvActor;
    }

    public ActorRef getMsgVerifyActor() {
        return msgVerifyActor;
    }

    public ActorRef getMsgFilterActor() {
        return msgFilterActor;
    }

    public ActorRef getMsgRouteActor() {
        return msgRouteActor;
    }

    public ActorRef getMsgSendActor() {
        return msgSendActor;
    }

    public ActorRef getMsgSaveActor() {
        return msgSaveActor;
    }

    public void build(Environment environment) throws Exception{
        environment.lifecycle().manage(this);
    }

    public void build(Environment environment, String proxyConf) throws Exception{
        this.proxyConf = proxyConf;
        environment.lifecycle().manage(this);
    }

    @Override
    public void start() throws Exception {
        try {
            // 初始化Actor
            Config config = ConfigFactory.load(proxyConf);
            actorSystem = ActorSystem.create("MsgProxy", config);
            msgRecvActor = actorSystem.actorOf(Props.create(MsgReceiverServiceImpl.class).withRouter(new FromConfig()), "msgReceiver");
            msgVerifyActor = actorSystem.actorOf(Props.create(MsgVerificationServiceImpl.class).withRouter(new FromConfig()), "msgVerification");
            msgFilterActor = actorSystem.actorOf(Props.create(MsgFilterServiceImpl.class).withRouter(new FromConfig()),"msgFilter");
            msgRouteActor = actorSystem.actorOf(Props.create(MsgRouterServiceImpl.class).withRouter(new FromConfig()), "msgRouter");
            msgSendActor = actorSystem.actorOf(Props.create(MsgSenderServiceImpl.class).withRouter(new FromConfig()), "msgSender");
            msgSaveActor = actorSystem.actorOf(Props.create(MsgSaverServiceImpl.class).withRouter(new FromConfig()), "msgStorage");

        } catch (Exception ex) {
            Log.error(MsgProxy.class.getName(), "start() error", ex);
        }
    }

    @Override
    public void stop() throws Exception {
        try {
            if (actorSystem != null) {
                actorSystem.shutdown();
            }
        } catch (Exception ex) {
            Log.error(MsgProxy.class.getName(), "stop() error", ex);
        }
    }
}
